#include "MainWnd.h"
#include "ui_MainWnd.h"
#include <QPainter>
#include <QVBoxLayout>
#include "custom/titlebar.h"
#include "custom/tablewidget.h"
#include "custom/framelesshelper.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | windowFlags());

    TitleBar *pTitleBar = new TitleBar(this);
    installEventFilter(pTitleBar);

//  TableWidget *pTabwidget = new TableWidget(this);
//  installEventFilter(pTabwidget);

    resize(800, 600);
    setWindowTitle("工控终端安全卫士");
    setWindowIcon(QIcon(":/Images/logo"));

    QPalette pal(palette());
    pal.setColor(QPalette::Background, QColor(220, 220, 220));
    setAutoFillBackground(true);
    setPalette(pal);

    QVBoxLayout *pLayout = new QVBoxLayout();
    pLayout->addWidget(pTitleBar);
//  pLayout->addWidget(pTabwidget);
    pLayout->addStretch();
    pLayout->setSpacing(0);
    pLayout->setContentsMargins(0, 0, 0, 0);
    setLayout(pLayout);

    int w = ui->tabWidget->width();
    //background: transparent;背景透明
    QString str = (tr("QTabBar::tab{width: %1px;height:46px;font:20px;color:black}").arg(w/3-20));
    str.append("QTabWidget::pane{border-width: 0px;border-color:white;border-style:outset;background-color: rgb(132, 171, 208);border-radius: 3px;}");
    //将tab-bar左移动
    str.append("QTabWidget::tab-bar{left:20px;}");
    //点击tab此时的状态
    //margin-left距离左边的tab边缘的距离，margin-right距离右边的tab边缘的距离，color是字体颜色，background-color背景色
    str.append("QTabBar::tab:selected{margin-left: 1;margin-right: 0;color: black;background-color:rbg(100,200,255);} ");
    str.append("QTabBar::tab:!selected{margin-left: 1;margin-right: 0;color: white;background-color:rbg(50,100,255);} ");
    str.append("QTabBar::tab:hover:!selected{color: white;margin-left: 1;margin-right: 0;background-color:rbg(100,200,100);} ");
//  str.append("QTabBar::tab:!selected{margin-top: 10px;margin-bottom: -10px;} ");
    ui->tabWidget->setStyleSheet(str);

//  FramelessHelper *pHelper = new FramelessHelper(this);
//  pHelper->activateOn(this);  //激活当前窗体
//  pHelper->setTitleHeight(pTitleBar->height());  //设置窗体的标题栏高度
//  pHelper->setWidgetMovable(true);  //设置窗体可移动
//  pHelper->setWidgetResizable(true);  //设置窗体可缩放
//  pHelper->setRubberBandOnMove(true);  //设置橡皮筋效果-可移动
//  pHelper->setRubberBandOnResize(true);  //设置橡皮筋效果-可缩放
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    QPixmap background(":/background");

    int nLeftWidth = 144;
    int nBottomHeight = 24;
    int nTopHeight = 67;

    // 分别计算左、上、右、下的区域
    QRect left(0, 100, nLeftWidth, 100);
    QRect right(background.width() - nLeftWidth, 100, nLeftWidth, 100);
    QRect leftTop(0, 0, nLeftWidth, nTopHeight);
    QRect rightTop(background.width() - nLeftWidth, 0, nLeftWidth, nTopHeight);
    QRect top(150, 0, 150, nTopHeight);
    QRect leftBottom(0, background.height() - nBottomHeight, nLeftWidth, nBottomHeight);
    QRect rightBottom(background.width() - nLeftWidth, background.height() - nBottomHeight, nLeftWidth, nBottomHeight);
    QRect bottom(150, background.height() - nBottomHeight, 100, nBottomHeight);
    QRect center(300, 300, 100, 100);

    QRect leftRect(0, nTopHeight, nLeftWidth, this->height() - nTopHeight - nBottomHeight);
    QRect rightRect(this->width() - nLeftWidth, nTopHeight, nLeftWidth, this->height() - nTopHeight - nBottomHeight);
    QRect leftTopRect(0, 0, nLeftWidth, nTopHeight);
    QRect rightTopRect(this->width() - nLeftWidth, 0, nLeftWidth, nTopHeight);
    QRect topRect(nLeftWidth, 0, this->width() - nLeftWidth*2, nTopHeight);
    QRect leftBottomRect(0, this->height() - nBottomHeight, nLeftWidth, nBottomHeight);
    QRect righttBottomRect(this->width() - nLeftWidth, this->height() - nBottomHeight, nLeftWidth, nBottomHeight);
    QRect bottomRect(nLeftWidth, this->height() - nBottomHeight, this->width() - nLeftWidth*2, nBottomHeight);
    QRect centerRect(nLeftWidth, nTopHeight, this->width() - nLeftWidth*2, this->height() - nTopHeight - nBottomHeight);

    // 绘制图片
    painter.drawPixmap(topRect, background, top);
    painter.drawPixmap(leftRect, background, left);
    painter.drawPixmap(rightRect, background, right);
    painter.drawPixmap(rightTopRect, background, rightTop);
    painter.drawPixmap(leftTopRect, background, leftTop);
    painter.drawPixmap(leftBottomRect, background, leftBottom);
    painter.drawPixmap(righttBottomRect, background, rightBottom);
    painter.drawPixmap(bottomRect, background, bottom);
    painter.drawPixmap(centerRect, background, center);
}

// 鼠标相对于窗体的位置 event->globalPos() - this->pos()
void Widget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_bPressed = true;
        m_point = event->pos();
    }
}

// 若鼠标左键被按下，移动窗体位置
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if (m_bPressed)
        move(event->pos() - m_point + pos());
}

// 设置鼠标未被按下
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);

    m_bPressed = false;
}


void Widget::on_Run_clicked()
{

}

void Widget::on_Stop_clicked()
{

}

void Widget::on_Apply_clicked()
{

}

void Widget::on_clear_clicked()
{

}
